import Vue from "vue";
import VueRouter from "vue-router";
import Dashboard from "@/views/dashboard.vue";
import { flatTreeData } from "@/utils";
//解决vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;
// 重写！
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  // 如果指定了成功或者失败的回调
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject);
  }
  // 没有指定成功或者失败的回调，要用catch处理
  return originalPush.call(this, location).catch((err) => {
    // 如果是重复导航产生的错误，不再向外传递错误
    if (VueRouter.isNavigationFailure(err)) {
      return err;
    }

    // 如果不是重复导航的错误，将错误向下传递
    return Promise.reject(err);
  });
};
VueRouter.prototype.replace = function (location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalReplace.call(this, location, onResolve, onReject);
  }
  return originalReplace.call(this, location).catch((err) => {
    if (VueRouter.isNavigationFailure(err)) {
      return err;
    }
    return Promise.reject(err);
  });
};
// 注册 【VueRouter】
Vue.use(VueRouter);

let routes = [
  //第三方嵌入通用入口
  {
    path: "/embed",
    name: "Embed",
    component: () =>
      import(/* webpackChunkName: "Embed" */ "@/views/embed.vue"),
    children: [],
  },
  //通用登录地址【域名下可访问】
  {
    path: "*/login",
    name: "Login",
    component: () =>
      import(/* webpackChunkName: "Login" */ "@/views/login/index.vue"),
    children: [],
  },
  //三方嵌入
  {
    path: "/callback",
    name: "callback",
    component: () =>
      import(/* webpackChunkName: "callback" */ "@/views/callback/index.vue"),
    children: [
      {
        path: "/callback/department",
        name: "/callback/department",
        component: () =>
          import(
            /* webpackChunkName: "Home" */ "@/views/callback/department.vue"
          ),
      },
    ],
  },
  {
    path: "/",
    name: "Dashboard",
    component: Dashboard,
    children: [
      {
        path: "/home",
        name: "Home",
        component: () =>
          import(/* webpackChunkName: "Home" */ "@/views/home/index.vue"),
      },

      {
        path: "/serviceView",
        name: "serviceView",
        component: () =>
          import(
            /* webpackChunkName: "serviceView" */ "@/views/serviceView/index.vue"
          ),
        children: [
          {
            path: "/iframe",
            name: "Iframe",
            component: () =>
              import(
                /* webpackChunkName: "Iframe" */ "@/views/placeholder/iframe.vue"
              ),
          },
          {
            path: "/developing",
            name: "Developing",
            component: () =>
              import(
                /* webpackChunkName: "Developing" */ "@/views/placeholder/developing.vue"
              ),
          },
          {
            path: "*",
            name: "NotFound",
            component: () =>
              import(
                /* webpackChunkName: "NotFound" */ "@/views/placeholder/notFound.vue"
              ),
          },
          {
            path: "/NotFound",
            name: "NotFound",
            component: () =>
              import(
                /* webpackChunkName: "NotFound" */ "@/views/placeholder/notFound.vue"
              ),
          },
          {
            path: "/todo",
            name: "Todo",
            component: () =>
              import(
                /* webpackChunkName: "Todo" */ "@/views/dataControl/todo/index.vue"
              ),
          },
        ],
      },
      {
        path: "/welcome",
        name: "Welcome",
        component: () =>
          import(
            /* webpackChunkName: "Welcome" */ "@/views/placeholder/welcome.vue"
          ),
      },
    ],
  },
];
const files = require.context("./modules", true, /\.js$/);
files.keys().forEach((key) => {
  if (files(key).default) {
    routes.forEach((v) => {
      if (v.name == "Dashboard") {
        let { children } = v;
        children.forEach((item) => {
          if (item.name == "serviceView") {
            if (!item.children) {
              item.children = [...files(key).default];
            } else {
              item.children.push(...files(key).default);
            }
          }
        });
      }
    });
  }
});

let { VUE_APP_CURRENTMODE } = process.env;
const router = new VueRouter({
  mode: "history",
  base: VUE_APP_CURRENTMODE == "build" ? "/dashboard/" : "/dashboard/",
  routes,
});
export { routes };
export default router;
